/*
 * PointHandle.cpp
 *
 *  Created on: 2010-12-12
 *      Author: lyyang
 */

#include "UseCount.h"
#include "PointHandle.h"
#include "Point.h"
#include <iostream>
using namespace std;

PointHandle::~PointHandle()
{
	if (u.only()) {
		delete p;
	}
}

PointHandle & PointHandle::operator =(const PointHandle & h)
{
	if (u.reattach(h.u)) {
		delete p;
	}
	p = h.p;
	return *this;
}

PointHandle & PointHandle::x(int v)
{
	if (u.makeonly()) {
		p = new Point(*p);
	}
	p->x(v);
	return *this;
}

PointHandle & PointHandle::y(int v)
{
	if (u.makeonly()) {
		p = new Point(*p);
	}
	p->y(v);
	return *this;
}

void PointHandle::report(ostream& os)
{
	os << "Point: (" << x() << ", " << y() << ")\t";
	os << "Use count: " << u.tell() << "\n";
}

int main(int argc, char *argv[])
{
	Point p(1,2);
	PointHandle h1(p);

	PointHandle h2(h1);
	h1.report();
	h2.report();

	PointHandle h3(3,4);
	h3.report();
	h3 = h1;
	h3.report();

	PointHandle h4(h3);
	h3.report();
	h4.report();

	h3.x(5).y(4);
	h1.report();
	h2.report();
	h3.report();
	h4.report();

	return 0;
}























